
<%@page import="it.trekker.dao.IUserDAO"%>
<%@page import="it.trekker.dao.DAOFactory"%>
<%@page import="org.apache.log4j.Logger"%>
<%@page import="java.util.List"%>
<%@page import="it.trekker.dao.User"%>
<%!
String printUserTableRow(User user) {
	
	if(user == null)
		return "";
	
	StringBuffer html = new StringBuffer();
	
	html
		.append("<tr>")
		.append("<td>")
		.append(user.getId())
		.append("</td>")
		.append("<td>")
		.append(user.getUsername())
		.append("</td>")
		.append("<td>")
		.append("<form method='POST' action='search.jsp'>")
		.append("<input type='hidden' name='searchType' value='username' />")
		.append("<input name='username' type='hidden' value=" + user.getUsername() + ">")
		.append("<input id='submit' type='submit' value='Show excursions'>")
		.append("</form>")
		.append("</td>")
		.append("</tr>");
	
	return html.toString(); 
}

String printUserTableRows(List<User> users) {
	if(users == null)
		return "";
	
	StringBuffer html = new StringBuffer();
	
	for(User user : users) {
		html.append(printUserTableRow(user));
	}	
	
	return html.toString(); 
}
%>

<%
Logger logger = Logger.getLogger("it.trekker.search.jsp");

DAOFactory daoFactory = DAOFactory.getDAOFactory(application.getInitParameter("dao"));
IUserDAO userDAO = daoFactory.getUserDAO();
%>

<html>
	<head>
		<title>Trekker</title>
	
		<meta http-equiv="Pragma" content="no-cache"/>
		<meta http-equiv="Expires" content="Mon, 01 Jan 1996 23:59:59 GMT"/>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
		<meta name="Author" content="you">

		<link rel="StyleSheet" href="styles/default.css" type="text/css" media="all" />
	</head>
	
	<body>
		<div id="menu_link">
			<a href="index.jsp">Back to home</a>
		</div>
		
		<h1>Users</h1>
		
		<div>
			<table class="excursions">
				<thead>
					<tr>
						<th>Id</th>
						<th>Username</th>
						<th>View excursions</th>
					</tr>
				</thead>
				<%=	printUserTableRows(userDAO.findAllUsers()) %> 
			</table>
		</div>
	</body>

</html>
